package com.bestcem.xm.user.grpc.service.converter;

import com.bestcem.bp.xm.user.grpc.v1.services.ListOrgCustomSettingsRequest;
import com.bestcem.bp.xm.user.grpc.v1.services.OrgCustomSetting;
import com.bestcem.xm.user.enums.BooleanStatusEnum;
import com.bestcem.xm.user.service.dto.user.OrganizationCustomSettingDTO;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 租户自定义配置转换类
 *
 * @author qiongyu.cui <qiongyu.cui@idiaoyan.com>
 * @version v1.0
 * @date 2022/5/10 13:48
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class OrganizationCustomSettingConvert {

    /**
     * 将请求信息转换为本地入参
     * @param request 请求信息
     * @return com.bestcem.xm.user.service.dto.user.OrganizationCustomSettingDTO
     * @author cuiqiongyu <qiongyu.cui@idiaoyan.cn>
     * @date 2022/05/10 13:53
     */
    public static OrganizationCustomSettingDTO settingToDto(ListOrgCustomSettingsRequest request) {
        OrganizationCustomSettingDTO settingDto = new OrganizationCustomSettingDTO();
        if (Objects.nonNull(request)) {
            settingDto.setId(request.getId());
            settingDto.setOrgId(request.getOrgId());
            settingDto.setEnable(BooleanStatusEnum.toInteger(request.getEnable()));
            settingDto.setDomain(request.getDomain());
            settingDto.setFavicon(request.getFavicon());
        }
        return settingDto;
    }

    /**
     * dto列表转成grpc需要的列表
     * @param dtos dto列表
     * @return java.util.List<com.bestcem.bp.xm.user.grpc.v1.services.OrgCustomSetting>
     * @author cuiqiongyu <qiongyu.cui@idiaoyan.cn>
     * @date 2022/05/10 14:02
     */
    public static List<OrgCustomSetting> dtosToSettings(List<OrganizationCustomSettingDTO> dtos) {
        List<OrgCustomSetting> settings = new ArrayList<>();
        for (OrganizationCustomSettingDTO dto : dtos) {
            OrgCustomSetting.Builder builder = OrgCustomSetting.newBuilder();
            builder.setId(dto.getId());
            builder.setDomain(dto.getDomain());
            builder.setFavicon(dto.getFavicon());
            builder.setEnable(Integer.valueOf(1).equals(dto.getEnable()));
            builder.setOrgId(dto.getOrgId());
            settings.add(builder.build());
        }
        return settings;
    }

}
